package com.strawcom.charmring.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;

import com.strawcom.charmring.util.LogUtil;

public class MProgressView extends View {
	public MProgressView(Context context) {
		this(context, null);
	}

	public MProgressView(Context context, AttributeSet attrs) {
		super(context, attrs);
		setLayerType(LAYER_TYPE_SOFTWARE, null);
		setLayoutParams(new android.view.ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
	}

	private static final int COLOR_ABLE = 0xffff81ae;
	private static final int COLOR_DISABLE = 0xffcccccc;

	private int mWidth, mHeight;
	private float percent;
	private float radius;
	private float shadowWidth;
	private float padding;

	private float thickness;

	private boolean enable = true;

	private final Paint paint = new Paint();

	private OnProgressChangedListener listener;

	private float minValue, maxValue, currentValue;

	public void setListener(OnProgressChangedListener listener) {
		this.listener = listener;
	}

	public interface OnProgressChangedListener {
		void onProgressChanged(float progress);
	}

	@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int widthSize = MeasureSpec.getSize(widthMeasureSpec);
		int heightSize = (int) (widthSize * 0.09f);
		setMeasuredDimension(widthSize, heightSize);
	}

	@Override protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
		LogUtil.d("ExerciseRerocdLayout onSizeChanged() w:" + w + ",h:" + h);
		mWidth = w;
		mHeight = h;
		radius = h * 0.42f;
		shadowWidth = radius * 0.03f;
		padding = radius + shadowWidth;
	}

	@Override protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		paint.setColor(enable ? COLOR_ABLE : COLOR_DISABLE);
		canvas.drawRect(padding, mHeight * (0.3f + 0.2f * thickness), mWidth - padding, mHeight * (0.7f - 0.2f * thickness), paint);
		paint.setAntiAlias(true);
		final float barCenter = radius + (mWidth - radius * 2) * percent;
		paint.setColor(0xffffffff);
		paint.setShadowLayer(shadowWidth, 2, 8, 0xffcccccc);
		canvas.drawCircle(barCenter, mHeight / 2, radius, paint);
		paint.clearShadowLayer();
		paint.setStyle(Style.STROKE);
		paint.setStrokeWidth(1);
		paint.setColor(0xffaaaaaa);
		canvas.drawCircle(barCenter, mHeight / 2, radius, paint);
		paint.reset();
	}

	@Override public boolean onTouchEvent(MotionEvent event) {
		if (enable) {
			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				return isTouchBaby(event);
			case MotionEvent.ACTION_MOVE:
				percent = (event.getX() - padding) / (mWidth - 2 * padding);
				if (percent > 1) {
					percent = 1;
				}
				else if (percent < 0) {
					percent = 0;
				}
				currentValue = (maxValue - minValue) * percent + minValue;
				if (listener != null) {
					listener.onProgressChanged(currentValue);
				}
				invalidate();
				break;
			}
		}
		return super.onTouchEvent(event);
	}

	private boolean isTouchBaby(MotionEvent event) {
		final float x = event.getX();
		final float barCenter = radius + (mWidth - radius * 2) * percent;
		final float comfortableWrap = mWidth * 0.06f;
		return x > barCenter - radius - comfortableWrap && x < barCenter + radius + comfortableWrap;
	}

	public synchronized void setCurrentValue(float value, float min, float max) {
		if (value < min || value > max || min < 0) {
			throw new IllegalArgumentException("setCurrentValue : error value " + value);
		}
		currentValue = value;
		minValue = min;
		maxValue = max;

		percent = (currentValue - minValue) * 1f / (maxValue - minValue);

		postInvalidate();
		if (listener != null) listener.onProgressChanged(currentValue);
	}

	public synchronized void setCurrentValue(float value) {
		currentValue = value < 0 ? 0 : value;

		if (currentValue == 0) {
			minValue = 0;
			maxValue = ExerciseRerocdLayout.MAX_WEIGHT;
		}
		else {
			minValue = currentValue - 5 < 0 ? 0 : currentValue - 5;
			maxValue = currentValue + 5;
		}

		setCurrentValue(currentValue, minValue, maxValue);
	}

	public float getCurrentValue() {
		return currentValue;
	}

	public boolean isEnable() {
		return enable;
	}

	public void setEnable(boolean enable) {
		this.enable = enable;
		postInvalidate();
	}

	/**
	 * 设置进度条厚度
	 * 
	 * @param thickness
	 *            0-1 如果为0比较粗， 如果为1非常细
	 */
	public void setThickness(float thickness) {
		if (thickness < 0) thickness = 0;
		else if (thickness > 1) thickness = 1;

		this.thickness = thickness;
		postInvalidate();
	}
}